﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DemoGame1
{
    public class My2DSprite
    {
        private Texture2D[] _Textures;
        private Vector2 _Size = new Vector2(0, 0);
        SpriteEffects _spriteEffects;

        public SpriteEffects SpriteEffects
        {
            get { return _spriteEffects; }
            set { _spriteEffects = value; }
        }
        public Vector2 Size
        {
            get { return _Size; }
            set { _Size = value; }
        }
        public Vector2 TrueSize;
        public Texture2D[] Textures
        {
            get { return _Textures; }
            set
            {
                _Textures = value;
                _nTextures = _Textures.Length;
                _iTextures = 0;
            }

        }
        private int _iTextures;
        private bool _visible = true;

        public bool Visible
        {
            get { return _visible; }
            set { _visible = value; }
        }
        public int iTextures
        {
            get { return _iTextures; }
            set { _iTextures = value; }
        }


        private int _nTextures;

        public int nTextures
        {
            get { return _nTextures; }
            set { _nTextures = value; }
        }


        private Vector2 _Position;

        public Vector2 Position
        {
            get { return _Position; }
            set { _Position = value; }
        }

        private Color _Color = Color.White;

        public Color Color
        {
            get { return _Color; }
            set { _Color = value; }
        }



        public My2DSprite(Texture2D[] textures, Vector2 position)
        {
            Textures = textures;
            Position = position;

        }
        private float _NormalDelay = 16.0f;

        public float NormalDelay
        {
            get { return _NormalDelay; }
            set { _NormalDelay = value; }
        }

        private float _Scale = 1.0f;

        public float Scale
        {
            get { return _Scale; }
            set { _Scale = value; }
        }
        private Vector2 _origin = Vector2.Zero;

        public Vector2 Origin
        {
            get { return _origin; }
            set { _origin = value; }
        }
        public void Update(GameTime gameTime)
        {
            int delta = (int)(gameTime.TotalGameTime.Milliseconds / _NormalDelay);

            _iTextures = (1 + _iTextures) % _nTextures;

        }
        public void SetNextFrame(int nFrame)
        {
            if (nFrame < 0)
            {
                _iTextures = Math.Max(0, nFrame + _iTextures);
            }
            if (nFrame > 0)
            {
                _iTextures = Math.Min(_nTextures - 1, nFrame + _iTextures);
            }
        }
        public void NextFrame()
        {
            _iTextures = (1 + _iTextures) % _nTextures;
        }

        public void Draw(SpriteBatch sb, GameTime gameTime)
        {
            //sb.Draw(_Textures[_iTextures], _Position, Color.White);
            Rectangle rect = new Rectangle();
            if (_Size.X == 0)
                rect = new Rectangle((int)_Position.X, (int)_Position.Y, _Textures[iTextures].Width, _Textures[iTextures].Height);
            else
                rect = new Rectangle((int)_Position.X, (int)_Position.Y, (int)_Size.X, (int)_Size.Y);
            // sb.Draw(_Textures[_iTextures], rect, Color.White);
            if (_visible)
                sb.Draw(_Textures[_iTextures], Position, null, Color.White, 0, _origin, Scale, SpriteEffects, 0f);

        }

        public void Draw(SpriteBatch sb, GameTime gameTime, Vector2 size)
        {
            Rectangle rect = new Rectangle();
            if (size.X == 0)
                rect = new Rectangle((int)_Position.X, (int)_Position.Y, _Textures[iTextures].Width, _Textures[iTextures].Height);
            else
                rect = new Rectangle((int)_Position.X, (int)_Position.Y, (int)size.X, (int)size.Y);
            if (_visible)
                sb.Draw(_Textures[_iTextures], rect, Color.White);

        }
        public void Draw(SpriteBatch sb, GameTime gameTime, Vector2 position, Color color)
        {
            sb.Draw(_Textures[_iTextures], position, color);
        }

        public void Draw(SpriteBatch sb, GameTime gameTime, Vector2 topleft, Vector2 size)
        {
            Rectangle rect = new Rectangle();
            rect = new Rectangle((int)topleft.X, (int)topleft.Y, (int)size.X, (int)size.Y);
            sb.Draw(_Textures[_iTextures], rect, _Color);
        }


        public void DrawwithDepth(SpriteBatch sb, GameTime gameTime, Vector2 topleft, Vector2 size, float depth)
        {
            Rectangle rect = new Rectangle();
            rect = new Rectangle((int)topleft.X, (int)topleft.Y, (int)size.X, (int)size.Y);
            sb.Draw(_Textures[_iTextures], rect,null, _Color,0f, new Vector2(0,0),SpriteEffects.None,depth);
           // sb.Draw(_Textures[_iTextures], _TopLeft, null, Color.White, 0, new Vector2(0,0), 1.0f, SpriteEffects.None, depth);
        }
    }
}
